package com.server.web.controller.officialAccounts;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.server.common.core.domain.AjaxResult;
import com.server.common.utils.StringUtils;
import com.server.pinggu.domain.SenPgy;
import com.server.pinggu.domain.SenStreets;
import com.server.pinggu.dto.UpdateUserInfoDTO;
import com.server.pinggu.dto.UpdateUserJuWeiDTO;
import com.server.pinggu.service.ISenPgyService;
import com.server.pinggu.service.ISenStreetsService;
import com.server.pinggu.service.ISenUserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname FengRui
 * @Description 居委相关接口
 * @Date 2022-11-16 10:39
 */
@RestController
@RequestMapping("/officialAccounts/street")
@AllArgsConstructor
public class SenStreetInfoController {
    private final ISenStreetsService iSenStreetsService;

    private final ISenUserService iSenUserService;

    private final ISenPgyService iSenPgyService;



    @ApiOperation(value = "获取街道信息")
    @PostMapping("getStreetInfo")
    public AjaxResult getUserInfoByOpenId(){
        return AjaxResult.success(iSenStreetsService.senStreetsVo(null));
    }


    @ApiOperation(value = "修改用户居委")
    @PostMapping("updateStreetInfo/{openId}")
    public AjaxResult updateStreetInfo(@PathVariable("openId")String openId,@RequestBody UpdateUserInfoDTO updateUserDTO){
        Long info = iSenUserService.getformsInfoByOpenId(openId);
        if (info!=null && updateUserDTO!=null&& info!=0){
            String streetIds =null;
            List<String> collect = updateUserDTO.getList().stream().filter(a -> StringUtils.isEmpty(a.getJuWeiName()) &&StringUtils.isNotEmpty(a.getStreetName())).collect(Collectors.toList()).stream().map(UpdateUserJuWeiDTO::getStreetName).collect(Collectors.toList());
            List<String> juWeiDTOS = updateUserDTO.getList().stream().filter(a -> StringUtils.isNotEmpty(a.getJuWeiName())).collect(Collectors.toList()).stream().map(UpdateUserJuWeiDTO::getJuWeiName).collect(Collectors.toList());
            collect.addAll(juWeiDTOS);
            List<String> distict = collect.stream().distinct().collect(Collectors.toList());
            List<SenStreets> list = iSenStreetsService.list(new LambdaQueryWrapper<SenStreets>().in(SenStreets::getTitle, distict).select(SenStreets::getId));
            List<Long> ids = list.stream().map(SenStreets::getId).filter(ObjectUtils::isNotEmpty).distinct().collect(Collectors.toList());
            for (Long id : ids) {
                if (StringUtils.isEmpty(streetIds)) {
                    streetIds = id.toString();
                }else{
                    streetIds = streetIds+","+id.toString();
                }
            }
            LambdaUpdateWrapper<SenPgy> senPgyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            senPgyLambdaUpdateWrapper.eq(SenPgy::getId, info);
            if (StringUtils.isNotEmpty(streetIds)){
                senPgyLambdaUpdateWrapper.set(SenPgy::getJuweiId, streetIds);
            }
            if (StringUtils.isNotEmpty(updateUserDTO.getSfzh())){
                senPgyLambdaUpdateWrapper.set(SenPgy::getZjhm, updateUserDTO.getSfzh());
            }
            iSenPgyService.update(senPgyLambdaUpdateWrapper);
        }else{
            return AjaxResult.error("绑定失败，评估员信息不存在");
        }
        return AjaxResult.success();
    }
}
